import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Index from '../views/Index.vue'
import Home from '../views/home/Home.vue'
import Users from '../views/users/Users.vue'
import Roles from '../views/rights/Roles.vue'
import Rights from '../views/rights/Rights.vue'
import Goods from '../views/products/Goods.vue'
import Params from '../views/products/Params.vue'
import Categories from '../views/products/Categories.vue'
import goodsAdd from '../views/products/goodsAdd.vue'
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/index' },
  { path: '/login', component: Login },
  { path: '/index',
    component: Index,
    children: [
      { path: '', component: Home },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/params', component: Params },
      { path: '/categories', component: Categories },
      { path: '/goods', component: Goods },
      { path: '/goods-add', component: goodsAdd }

    ]
  }
]

const router = new VueRouter({
  routes
})
// 配置全局前置导航守卫
router.beforeEach((to, from, next) => {
// 获取token
  const token = localStorage.getItem('token')
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})
export default router
